﻿using Authority.Model;
using Common.Library;
using Container.Library;
using Dapper.Library;
using Dapper.MsSql.Library;
using Document.Library;
using Network.Library;
using Project.Model;
using Redis.Library;
using Dapper.Library.DataAccessSql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tool.Model;

namespace Project.Logic
{
    /// <summary>
    /// 员工操作逻辑类
    /// </summary>
    public class EmployeeServiceImpl : IEmployeeService
    {
        #region 员工信息相关

        /// <summary>
        /// 获取员工列表信息
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<T_EmployeeEntity>> LoadEmployeeList(ParametersInfo<EmployeeInfoQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<T_EmployeeEntity>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = con.QuerySet<T_EmployeeEntity>().WithNoLock()
                    .AndWhere(p => p.IsDel == 0);

                #region 条件

                if (inputInfo.parameters.IsNotNull())
                {
                    if (inputInfo.parameters.KeyWord.IsNotNullOrEmpty())
                    {
                        result.AndWhere(p => p.Name.Contains(inputInfo.parameters.KeyWord) || p.PYM.Contains(inputInfo.parameters.KeyWord) || p.WorkType.Contains(inputInfo.parameters.KeyWord));
                    }
                    if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                    {
                        result.AndWhere(p => p.ProjectNo.Contains(inputInfo.parameters.ProjectNo));
                    }

                    if (inputInfo.parameters.OrgaizeName.IsNotNullOrEmpty())
                    {
                        result.AndWhere(p => p.OrgaizeName.Contains(inputInfo.parameters.OrgaizeName));
                    }
                    if (inputInfo.parameters.ZTBS != null)
                    {
                        if (inputInfo.parameters.ZTBS == 1)
                        {
                            result.AndWhere(p => p.ZTBS == true);
                        }
                        else
                        {
                            result.AndWhere(p => p.ZTBS == false);
                        }
                    }

                    if (inputInfo.parameters.CheckZT != null)
                    {
                        result.AndWhere(p => p.CheckZT == inputInfo.parameters.CheckZT);
                    }

                    if (inputInfo.parameters.MID.IsNotNull())
                    {
                        result.AndWhere(p => p.MID == inputInfo.parameters.MID);
                    }
                }

                #endregion
                result.AndWhere(p => p.DepNo == loginUser.OrganizationCode);

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field)
                    .PageList(inputInfo.page, inputInfo.limit);

                if (listInfo.Data.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.Data.ToList();
                    resultInfo.Count = listInfo.Total;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }


        /// <summary>
        /// 获取员工列表信息【用于导出使用】
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<T_EmployeeEntity>> LoadEmployeeAllList(ParametersInfo<EmployeeInfoQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<T_EmployeeEntity>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = con.QuerySet<T_EmployeeEntity>().WithNoLock()
                    .AndWhere(p => p.IsDel == 0);

                #region 条件

                if (inputInfo.parameters.IsNotNull())
                {
                    if (inputInfo.parameters.KeyWord.IsNotNullOrEmpty())
                    {
                        result.AndWhere(p => p.Name.Contains(inputInfo.parameters.KeyWord) || p.PYM.Contains(inputInfo.parameters.KeyWord) || p.WorkType.Contains(inputInfo.parameters.KeyWord));
                    }
                    if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.ProjectNo.Contains(inputInfo.parameters.ProjectNo));
                    }

                    if (inputInfo.parameters.OrgaizeName.IsNotNullOrEmpty())
                    {
                        result.AndWhere(p => p.OrgaizeName.Contains(inputInfo.parameters.OrgaizeName));
                    }

                    if (inputInfo.parameters.ZTBS != null)
                    {
                        if (inputInfo.parameters.ZTBS == 1)
                        {
                            result.AndWhere(p => p.ZTBS == true);
                        }
                        else
                        {
                            result.AndWhere(p => p.ZTBS == false);
                        }
                    }
                    if (inputInfo.parameters.CheckZT != null)
                    {
                        result = result.AndWhere(p => p.CheckZT == inputInfo.parameters.CheckZT);
                    }

                    if (inputInfo.parameters.MID.IsNotNull())
                    {
                        result = result.AndWhere(p => p.MID == inputInfo.parameters.MID);
                    }
                }

                #endregion
                result.AndWhere(p => p.DepNo == loginUser.OrganizationCode);

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field).Multiple();

                if (listInfo.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.ToList();
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 上传员工图片（后期把图片上传统一处理）
        /// </summary>
        /// <param name="webRootPath"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<string> UploadImage(string webRootPath, Dictionary<string, object> inputInfo)
        {
            var result = new ResultJsonInfo<string>();
            var imageBase64 = inputInfo["imageBase64"].ToString();
            var imageType = inputInfo["imageType"].ToString();
            result.Data = ImageHelper.SaveBase64Image(imageBase64, imageType, webRootPath);
            result.Code = ActionCodes.Success;
            return result;
        }


        /// <summary>
        /// 获取员工图片信息
        /// </summary>
        /// <returns></returns>
        public ResultJsonInfo<T_EmployeeImgEntity> LoadEmployeeImg(string userNo)
        {

            var resultInfo = new ResultJsonInfo<T_EmployeeImgEntity>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = con.QuerySet<T_EmployeeImgEntity>().WithNoLock()
                    .AndWhere(p => p.UserNo == userNo).Single();

                if (result.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = result;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }


        #region 保存信息
        /// <summary>
        /// 保存员工信息
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> Save(EmployeeOperateRequest inputInfo)
        {
            var resultInfo = new ResultJsonInfo<int>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();
            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = 0;
                var employeeInfo = inputInfo.MapTo<T_EmployeeEntity>();

                con.Transaction(tran =>
                {
                    var projectInfo = tran.QuerySet<T_ProjectEntity>().WithNoLock().AndWhere(p => p.ProjectNo == employeeInfo.ProjectNo).Single();
                    employeeInfo.ProjectName = projectInfo.ProjectName;

                    if (employeeInfo.MID != 0)
                    {
                        employeeInfo.MName = EnumExtentions.GetEnumDescriptionByValue(Convert.ToInt32(employeeInfo.MID), typeof(EmployeeType));
                    }

                    if (inputInfo.ID > 0)
                    {
                        tran.CommandSql<int>().BySql(DataCommandManager.GetSQLCommand("UpdateEmployeeImg", "Employee"),
                             new
                             {
                                 UserNo = inputInfo.UserNo,
                                 FilePath = inputInfo.PicUrl
                             }).ExecuteNoQuery();

                        result = tran.CommandSql<int>().BySql(DataCommandManager.GetSQLCommand("UpdateEmployeeByExp", "Employee"),
                            new
                            {
                                ID = employeeInfo.ID,
                                Name = employeeInfo.Name,
                                Code = employeeInfo.Code,
                                Sex = employeeInfo.Sex,
                                Post = employeeInfo.Post,
                                MID = employeeInfo.MID,
                                MName = employeeInfo.MName,
                                Address = employeeInfo.Address,
                                Phone = employeeInfo.Phone,
                                CrashPerson = employeeInfo.CrashPerson,
                                CrashPhone = employeeInfo.CrashPhone,
                                OrgaizeName = employeeInfo.OrgaizeName,
                                WorkType = employeeInfo.WorkType,
                                ProjectNo = employeeInfo.ProjectNo,
                                ProjectName = employeeInfo.ProjectName,
                                MonthMoney = employeeInfo.MonthMoney,
                                DayMoney = employeeInfo.DayMoney,
                                OverMoney = employeeInfo.OverMoney,
                                Descrption = employeeInfo.Descrption,
                                BankName = employeeInfo.BankName,
                                BankNo = employeeInfo.BankNo,
                                Credit = employeeInfo.Credit,
                                PostMoney = employeeInfo.PostMoney,
                                SocialMoney = employeeInfo.SocialMoney,
                                HouseMoney = employeeInfo.HouseMoney,
                                UpdateTime = DateTime.Now,
                                ZTBS = employeeInfo.ZTBS
                            }).ExecuteNoQuery();

                        tran.CommandSql<int>().BySql(DataCommandManager.GetSQLCommand("UpdateEmployeeImgByExp", "Employee"),
                         new
                         {
                             UserNo = employeeInfo.UserNo,
                             FilePath = inputInfo.PicUrl

                         }).ExecuteNoQuery();
                    }
                    else
                    {

                        employeeInfo.DepNo = loginUser.OrganizationCode;
                        employeeInfo.UserNo = GetNumber().ToString();
                        employeeInfo.CreateTime = DateTime.Now;
                        employeeInfo.IsDel = 0;
                        employeeInfo.CheckZT = 0;
                        employeeInfo.PYM = StringHelper.GetFirstPinyin(employeeInfo.Name);


                        tran.CommandSet<T_EmployeeImgEntity>().Insert(new T_EmployeeImgEntity
                        {
                            DepNo = employeeInfo.DepNo,
                            UserNo = employeeInfo.UserNo,
                            FilePath = inputInfo.PicUrl,
                            CreateTime = DateTime.Now
                        });

                        result = tran.CommandSet<T_EmployeeEntity>().Insert(new T_EmployeeEntity
                        {
                            DepNo = employeeInfo.DepNo,
                            Name = employeeInfo.Name,
                            PYM = employeeInfo.PYM,
                            UserNo = employeeInfo.UserNo,
                            Code = employeeInfo.Code,
                            Sex = employeeInfo.Sex,
                            Post = employeeInfo.Post,
                            MID = employeeInfo.MID,
                            MName = employeeInfo.MName,
                            Address = employeeInfo.Address,
                            Phone = employeeInfo.Phone,
                            CrashPerson = employeeInfo.CrashPerson,
                            CrashPhone = employeeInfo.CrashPhone,
                            OrgaizeName = employeeInfo.OrgaizeName,
                            WorkType = employeeInfo.WorkType,
                            WorkTypeID = 0,
                            ProjectNo = employeeInfo.ProjectNo,
                            ProjectName = employeeInfo.ProjectName,
                            MonthMoney = employeeInfo.MonthMoney,
                            DayMoney = employeeInfo.DayMoney,
                            OverMoney = employeeInfo.OverMoney,
                            Descrption = employeeInfo.Descrption,
                            BankName = employeeInfo.BankName,
                            BankNo = employeeInfo.BankNo,
                            Credit = employeeInfo.Credit,
                            PostMoney = employeeInfo.PostMoney,
                            SocialMoney = employeeInfo.SocialMoney,
                            HouseMoney = employeeInfo.HouseMoney,
                            CheckZT = employeeInfo.CheckZT,
                            IsDel = employeeInfo.IsDel,
                            CreateTime = employeeInfo.CreateTime
                        });
                    }
                    if (result > 0)
                    {
                        resultInfo.Code = ActionCodes.Success;
                        resultInfo.Data = result;
                        resultInfo.Msg = "操作成功！";
                    }
                    else
                    {
                        resultInfo.Msg = "操作失败！";
                    }
                }, ex =>
                {
                    throw ex;
                });
            }
            return resultInfo;
        }

        /// <summary>
        /// 在职与离职操作
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> Forbidden(int id)
        {

            var resultInfo = new ResultJsonInfo<int>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                con.Transaction(tran =>
                {
                    var employeeInfo = tran.QuerySet<T_EmployeeEntity>().AndWhere(p => p.ID == id).Single();

                    if (employeeInfo.IsNotNull())
                    {
                        employeeInfo.ZTBS = employeeInfo.ZTBS ? false : true;
                        int result = tran.CommandSet<T_EmployeeEntity>().Update(employeeInfo);
                        if (result > 0)
                        {
                            resultInfo.Code = ActionCodes.Success;
                            resultInfo.Data = result;
                            resultInfo.Msg = "操作成功！";
                        }
                        else
                        {
                            resultInfo.Msg = "操作失败！";
                        }
                    }
                    else
                    {
                        resultInfo.Msg = "操作失败,无对应信息！";
                    }
                });
            }
            return resultInfo;
        }

        /// <summary>
        /// 审核操作
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> AuditInfo(EmployeeAuditRequest inputInfo)
        {
            var resultInfo = new ResultJsonInfo<int>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                con.Transaction(tran =>
                {
                    var employeeInfo = tran.QuerySet<T_EmployeeEntity>()
                                 .AndWhere(p => inputInfo.ids.Contains(p.ID)).Multiple();

                    if (employeeInfo.IsNotNull() && employeeInfo.Count > 0)
                    {
                        int result = 0;
                        foreach (var item in employeeInfo)
                        {
                            item.CheckUserNo = loginUser.Code;
                            item.CheckUserName = loginUser.UserName;
                            item.CheckTime = DateTime.Now;
                            //通过
                            if (inputInfo.iType == (int)AuditStatus.AuditPass)
                            {
                                item.CheckZT = (int)AuditStatus.AuditPass;
                                item.ZTBS = true;
                            }
                            else
                            {
                                item.CheckZT = (int)AuditStatus.AudiFailure;
                                item.ZTBS = false;
                            }
                            result = tran.CommandSql<int>().BySql(DataCommandManager.GetSQLCommand("AuditEmployeeByExp", "Employee"),
                                      new
                                      {
                                          item.ID,
                                          item.CheckZT,
                                          item.CheckUserNo,
                                          item.CheckUserName,
                                          item.CheckTime,
                                          item.ZTBS

                                      }).ExecuteNoQuery();
                        }

                        if (result > 0)
                        {
                            resultInfo.Code = ActionCodes.Success;
                            resultInfo.Data = result;
                            resultInfo.Msg = "操作成功！";
                        }
                        else
                        {
                            resultInfo.Msg = "操作失败！";
                        }
                    }
                    else
                    {
                        resultInfo.Msg = "操作失败,无对应信息！";
                    }
                });
            }
            return resultInfo;
        }

        #endregion

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> Remove(List<int> IdList)
        {

            var resultInfo = new ResultJsonInfo<int>();
            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = 0;
                if (IdList.Count >= 0)
                {

                    ///IsDel字段的属性，不能再类型后面加?
                    result = con.CommandSet<T_EmployeeEntity>()
                        .AndWhere(p => IdList.Contains(p.ID))
                        .UpdateByExp(p => new T_EmployeeEntity { IsDel = 1 });

                    if (result > 0)
                    {
                        resultInfo.Code = ActionCodes.Success;
                        resultInfo.Data = result;
                        resultInfo.Msg = "操作成功！";
                    }
                    else
                    {
                        resultInfo.Msg = "操作失败！";
                    }
                }
                else
                {
                    resultInfo.Msg = "ID为空，操作失败！";
                }
            }
            return resultInfo;
        }

        #endregion

        #region 员工考情相关
        /// <summary>
        /// 获取考勤明细
        /// </summary>
        /// <param name="parametersInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<Dictionary<string, object>>> LoadExamineList(ParametersInfo<ExamineQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<Dictionary<string, object>>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                #region 条件

                whereStr.Append(" AND  Employee.DepNo = '" + loginUser.OrganizationCode + "'");
                whereStr.Append(" AND  Employee.CheckZT =1");

                if (inputInfo.parameters.KeyWord.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND  (Employee.Name like '%" + inputInfo.parameters.KeyWord + "%' OR Employee.PYM like '%" + inputInfo.parameters.KeyWord + "%')");
                }
                if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND  Employee.ProjectNo = '" + inputInfo.parameters.ProjectNo + "'");
                }

                int ztbs = 1;
                if (inputInfo.parameters.iType == 11)
                {
                    ztbs = 0;
                }
                #endregion

                #region 天数SQL拼装
                StringBuilder monthStr = new StringBuilder();
                DateTime dtNow = Convert.ToDateTime(inputInfo.parameters.YearMothDate);
                int days = DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
                string index = string.Empty;
                for (int i = 1; i <= days; i++)
                {
                    if (i < 10)
                    {
                        index = "0" + i.ToString();
                    }
                    else
                    {
                        index = i.ToString();
                    }
                    monthStr.Append(" ,SUM(CASE WHEN Attendance.WorkTime = '" + inputInfo.parameters.YearMothDate + "-" + index + "' THEN Attendance.WorkCount ELSE 0 END ) AS '" + i + "'");
                }

                #endregion

                string sqlStr = string.Format(DataCommandManager.GetSQLCommand("LoadExamineList", "Employee"), inputInfo.parameters.YearMothDate, monthStr, inputInfo.parameters.YearMothDate.Replace("-", ""), ztbs, whereStr);

                var result = con.QuerySql<Dictionary<string, object>>().BySql(sqlStr, null);

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field)
                  .PageList(inputInfo.page, inputInfo.limit);

                if (listInfo.Data.IsNotNull())
                {
                    var resutInfos = listInfo.Data.ToList();
                    foreach (var item in resutInfos)
                    {
                        string indexStr = string.Empty;

                        for (int i = 1; i <= days; i++)
                        {
                            indexStr = i > 9 ? i.ToString() : "0" + i.ToString();

                            if (Convert.ToDouble(item[i.ToString()]) == 0)
                            {
                                item[i.ToString()] = "<div style='color:#00C1FF' class='AttendanceMachine' data-userno='" + item["UserNo"] + "'  data-worktime='" + item["YearMothDate"] + "-" + indexStr + "'>" + item[i.ToString()] + "</div>";
                            }
                            else if (Convert.ToDouble(item[i.ToString()]) == 1)
                            {
                                item[i.ToString()] = "<div style='color:#008200' class='AttendanceMachine'  data-userno='" + item["UserNo"] + "'  data-worktime='" + item["YearMothDate"] + "-" + indexStr + "'>" + item[i.ToString()] + "</div>";
                            }
                            else
                            {
                                item[i.ToString()] = "<div style='color:#EE4B55'  class='AttendanceMachine' data-userno='" + item["UserNo"] + "'  data-worktime='" + item["YearMothDate"] + "-" + indexStr + "'>" + item[i.ToString()] + "</div>";
                            }
                        }
                    }
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = resutInfos;
                    resultInfo.Count = listInfo.Total;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取考勤不分页明细
        /// </summary>
        /// <param name="parametersInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<Dictionary<string, object>>> LoadExamineAllList(ParametersInfo<ExamineQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<Dictionary<string, object>>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                #region 条件

                whereStr.Append(" AND  Employee.DepNo = '" + loginUser.OrganizationCode + "'");
                whereStr.Append(" AND  Employee.CheckZT =1");

                if (inputInfo.parameters.KeyWord.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND  (Employee.Name like '%" + inputInfo.parameters.KeyWord + "%' OR Employee.PYM like '%" + inputInfo.parameters.KeyWord + "%')");
                }
                if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND  Employee.ProjectNo = '" + inputInfo.parameters.ProjectNo + "'");
                }

                int ztbs = 1;
                if (inputInfo.parameters.iType == 11)
                {
                    ztbs = 0;
                }

                #endregion

                #region 天数SQL拼装
                StringBuilder monthStr = new StringBuilder();
                DateTime dtNow = Convert.ToDateTime(inputInfo.parameters.YearMothDate);
                int days = DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
                string index = string.Empty;
                for (int i = 1; i <= days; i++)
                {
                    if (i < 10)
                    {
                        index = "0" + i.ToString();
                    }
                    else
                    {
                        index = i.ToString();
                    }
                    monthStr.Append(" ,SUM(CASE WHEN Attendance.WorkTime = '" + inputInfo.parameters.YearMothDate + "-" + index + "' THEN Attendance.WorkCount ELSE 0 END ) AS '" + i + "'");
                }

                #endregion

                string sqlStr = string.Format(DataCommandManager.GetSQLCommand("LoadExamineList", "Employee"), inputInfo.parameters.YearMothDate, monthStr, inputInfo.parameters.YearMothDate.Replace("-", ""), ztbs, whereStr);

                var result = con.QuerySql<Dictionary<string, object>>().BySql(sqlStr, null);

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field).Multiple();

                if (listInfo.IsNotNull() && listInfo.Count > 0)
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取考勤月报
        /// </summary>
        /// <param name="parametersInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<Dictionary<string, object>>> LoadExamineStatisticsList(ParametersInfo<ExamineStatisticsQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<Dictionary<string, object>>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                #region 条件
                whereStr.Append(" AND  Employee.DepNo = '" + loginUser.OrganizationCode + "'");
                whereStr.Append(" AND  Employee.CheckZT =1");
                whereStr.Append(" AND  Employee.ZTBS =1");

                if (inputInfo.parameters.KeyWord.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND ( Employee.Name like '%" + inputInfo.parameters.KeyWord + "%' OR Employee.PYM like '%" + inputInfo.parameters.KeyWord + "%')");
                }
                if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND  Employee.ProjectNo = '" + inputInfo.parameters.ProjectNo + "'");
                }
                #endregion

                //月份SQL拼装
                StringBuilder monthStr = new StringBuilder();
                var monthTimeLis = TimeHelper.GetMonthTimeList(inputInfo.parameters.dBeginTime, inputInfo.parameters.dEndTime);
                string index = string.Empty;
                foreach (var item in monthTimeLis)
                {
                    monthStr.Append(" ,SUM(CASE WHEN CONVERT(VARCHAR(6),Attendance.WorkTime,112) = '" + item.Replace("-", "") + "' THEN Attendance.WorkCount ELSE 0 END ) AS '" + item + "'");
                }
                string sqlStr = string.Format(DataCommandManager.GetSQLCommand("LoadExamineStatisticsList", "Employee"), monthStr, inputInfo.parameters.dBeginTime, inputInfo.parameters.dEndTime, whereStr);

                var result = con.QuerySql<Dictionary<string, object>>().BySql(sqlStr, null);

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field)
                  .PageList(inputInfo.page, inputInfo.limit);

                if (listInfo.Data.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.Data.ToList();
                    resultInfo.Count = listInfo.Total;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 获所有取考勤月报
        /// </summary>
        /// <param name="parametersInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<Dictionary<string, object>>> LoadExamineStatisticsAllList(ParametersInfo<ExamineStatisticsQueryRequest> inputInfo)
        {
            var resultInfo = new ResultJsonInfo<List<Dictionary<string, object>>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                #region 条件
                whereStr.Append(" AND  Employee.DepNo = '" + loginUser.OrganizationCode + "'");
                whereStr.Append(" AND  Employee.CheckZT =1");
                whereStr.Append(" AND  Employee.ZTBS =1");

                if (inputInfo.parameters.KeyWord.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND ( Employee.Name like '%" + inputInfo.parameters.KeyWord + "%' OR Employee.PYM like '%" + inputInfo.parameters.KeyWord + "%')");
                }
                if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND  Employee.ProjectNo = '" + inputInfo.parameters.ProjectNo + "'");
                }
                #endregion

                //月份SQL拼装
                StringBuilder monthStr = new StringBuilder();
                var monthTimeLis = TimeHelper.GetMonthTimeList(inputInfo.parameters.dBeginTime, inputInfo.parameters.dEndTime);
                foreach (var item in monthTimeLis)
                {
                    monthStr.Append(" ,SUM(CASE WHEN CONVERT(VARCHAR(6),Attendance.WorkTime,112) = '" + item.Replace("-", "") + "' THEN Attendance.WorkCount ELSE 0 END ) AS '" + item + "'");
                }
                string sqlStr = string.Format(DataCommandManager.GetSQLCommand("LoadExamineStatisticsList", "Employee"), monthStr, inputInfo.parameters.dBeginTime, inputInfo.parameters.dEndTime, whereStr);

                var listInfo = con.QuerySql<Dictionary<string, object>>().BySql(sqlStr, null).Multiple();

                if (listInfo.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.ToList();
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取打卡记录分页列表
        /// </summary>
        /// <param name="parametersInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<Dictionary<string, object>>> LoadMapAttendList(ParametersInfo<MapAttendQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<Dictionary<string, object>>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                #region 条件
                whereStr.Append(" AND  Employee.DepNo = '" + loginUser.OrganizationCode + "'");
                whereStr.Append(" AND  Employee.CheckZT =1");

                if (inputInfo.parameters.KeyWord.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND ( Employee.Name like '%" + inputInfo.parameters.KeyWord + "%' OR Employee.PYM like '%" + inputInfo.parameters.KeyWord + "%')");
                }
                if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                {
                    whereStr.Append(" AND  Employee.ProjectNo = '" + inputInfo.parameters.ProjectNo + "'");
                }

                if (inputInfo.parameters.dBeginTime.IsNotNull())
                {
                    whereStr.Append(" AND  MapAttend.AttTime >= '" + inputInfo.parameters.dBeginTime + "'");
                }
                if (inputInfo.parameters.dEndTime.IsNotNull())
                {
                    whereStr.Append(" AND  MapAttend.AttTime <= '" + inputInfo.parameters.dEndTime + "'");
                }
                #endregion

                var result = con.QuerySql<Dictionary<string, object>>().BySql(string.Format(DataCommandManager.GetSQLCommand("LoadClockOnRecordList", "Employee"), whereStr), null);

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field)
                  .PageList(inputInfo.page, inputInfo.limit);

                if (listInfo.Data.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.Data.ToList();
                    resultInfo.Count = listInfo.Total;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 更具日期和员工编号获取指定的考情信息
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<T_AttendanceEntity>> LoadDesignatedExamine(ParametersInfo<ExamineDesignatedQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<T_AttendanceEntity>>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = con.QuerySet<T_AttendanceEntity>().AndWhere(p => p.IsDel == 0).WithNoLock();
                #region 条件
                if (inputInfo.parameters.IsNotNull())
                {

                    if (inputInfo.parameters.UserNo.IsNotNullOrEmpty())
                    {
                        result.AndWhere(p => p.UserNo == inputInfo.parameters.UserNo);
                    }
                    if (inputInfo.parameters.WorkTime != null)
                    {
                        result.AndWhere(p => p.WorkTime == inputInfo.parameters.WorkTime);
                    }
                }
                #endregion

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field)
                    .PageList(inputInfo.page, inputInfo.limit);

                if (listInfo.Data.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.Data.ToList();
                    resultInfo.Count = listInfo.Total;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除考情信息
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> RemoveAttendance(List<int> IdList)
        {

            var resultInfo = new ResultJsonInfo<int>();
            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = 0;
                if (IdList.Count >= 0)
                {

                    ///IsDel字段的属性，不能再类型后面加?
                    result = con.CommandSet<T_AttendanceEntity>()
                        .AndWhere(p => IdList.Contains(p.ID))
                        .UpdateByExp(p => new T_AttendanceEntity { IsDel = 1 });

                    if (result > 0)
                    {
                        resultInfo.Code = ActionCodes.Success;
                        resultInfo.Data = result;
                        resultInfo.Msg = "操作成功！";
                    }
                    else
                    {
                        resultInfo.Msg = "操作失败！";
                    }
                }
                else
                {
                    resultInfo.Msg = "ID为空，操作失败！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 修改具体考情信息
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> ModifyAttendance(AttendanceModifyRequest inputInfo)
        {

            var resultInfo = new ResultJsonInfo<int>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                con.Transaction(tran =>
                {
                    var employeeInfo = tran.QuerySet<T_AttendanceEntity>()
                                 .AndWhere(p => p.ID == inputInfo.ID).Single();

                    if (employeeInfo.IsNotNull())
                    {
                        var projectInfo = tran.QuerySet<T_ProjectEntity>().WithNoLock().AndWhere(p => p.ProjectNo == inputInfo.ProjectNo).Single();

                        employeeInfo.WorkCount = inputInfo.WorkCount;
                        employeeInfo.WorkTime = inputInfo.WorkTime;
                        employeeInfo.Year = inputInfo.WorkTime.Year;
                        employeeInfo.ProjectNo = inputInfo.ProjectNo;
                        employeeInfo.ProjectName = projectInfo.ProjectName;
                        employeeInfo.WorkContent = inputInfo.WorkContent;
                        employeeInfo.UpdateTime = DateTime.Now;

                        int result = tran.CommandSet<T_AttendanceEntity>().Update(employeeInfo);

                        if (result > 0)
                        {
                            resultInfo.Code = ActionCodes.Success;
                            resultInfo.Data = result;
                            resultInfo.Msg = "操作成功！";
                        }
                        else
                        {
                            resultInfo.Msg = "操作失败！";
                        }
                    }
                    else
                    {
                        resultInfo.Msg = "操作失败,无对应信息！";
                    }
                });
            }
            return resultInfo;
        }

        /// <summary>
        /// 考情 审核通过
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> AuditInfo(List<AttendanceAuditRequest> auditInfo)
        {

            var resultInfo = new ResultJsonInfo<int>();
            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = 0;
                if (auditInfo.Count >= 0)
                {
                    DateTime monthFirstDay = GetCurMonthFirstDay(auditInfo[0].YearMothDate);
                    DateTime monthLastDay = GetCurMonthLastDay(auditInfo[0].YearMothDate);

                    List<string> UserNos = auditInfo.Select(p => p.UserNo).ToList();

                    result = con.CommandSet<T_AttendanceEntity>()
                        .AndWhere(p => UserNos.Contains(p.UserNo) && p.WorkTime.WhereBetween(monthFirstDay, monthLastDay))
                        .UpdateByExp(p => new T_AttendanceEntity { ZTBS = 1 });

                    if (result > 0)
                    {
                        resultInfo.Code = ActionCodes.Success;
                        resultInfo.Data = result;
                        resultInfo.Msg = "操作成功！";
                    }
                    else
                    {
                        resultInfo.Msg = "操作失败！";
                    }
                }
                else
                {
                    resultInfo.Msg = "操作失败！";
                }
            }
            return resultInfo;
        }

        private DateTime GetCurMonthFirstDay(string yearAndmonth)
        {
            DateTime AssemblDate = Convert.ToDateTime(yearAndmonth + "-" + "01");  // 组装当前指定月份
            return AssemblDate.AddDays(1 - AssemblDate.Day);  // 返回指定当前月份的第一天
        }
        private DateTime GetCurMonthLastDay(string yearAndmonth)
        {
            DateTime AssemblDate = Convert.ToDateTime(yearAndmonth + "-" + "01");  // 组装当前指定月份
            return AssemblDate.AddDays(1 - AssemblDate.Day).AddMonths(1).AddDays(-1);  // 返回指定当前月份的最后一天
        }

        #endregion

        #region 员工工资

        /// <summary>
        /// 获取员工工资统计信息【工资结算】
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<SalaryInfoQueryResponse>> LoadEmployeeSalaryList(ParametersInfo<SalaryQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<SalaryInfoQueryResponse>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                var listInfo = con.QuerySql<SalaryInfoQueryResponse>().BySql(DataCommandManager.GetSQLCommand("LoadEmployeeSalaryList", "Employee"),
                    new
                    {
                        Name = inputInfo.parameters.Name.IsNotNullOrEmpty() ? inputInfo.parameters.Name : null,
                        PYM = inputInfo.parameters.PYM.IsNotNullOrEmpty() ? inputInfo.parameters.PYM : null,
                        DepNO = loginUser.OrganizationCode,
                        ProjectNo = inputInfo.parameters.ProjectNo.IsNotNullOrEmpty() ? inputInfo.parameters.ProjectNo : null,
                        StartTime = inputInfo.parameters.dBeginTime,
                        EndTime = inputInfo.parameters.dEndTime,
                        WorkType = 0
                    }).Multiple();

                int count = 0;
                if (listInfo.Count > 0)
                {
                    count = listInfo.Count;
                    listInfo = listInfo.Skip((inputInfo.page - 1) * inputInfo.limit).Take(inputInfo.limit).ToList();
                    foreach (var item in listInfo)
                    {
                        item.ProjectNo = inputInfo.parameters.ProjectNo;
                        item.dBeginTime = inputInfo.parameters.dBeginTime;
                        item.dEndTime = inputInfo.parameters.dEndTime;
                    }
                }
                if (listInfo.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo;
                    resultInfo.Count = count;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 结算工资
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> Settlement(SalarySettlementRequest inputInfo)
        {

            var resultInfo = new ResultJsonInfo<int>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                con.Transaction(tran =>
                {
                    var employeeInfo = tran.QuerySet<T_EmployeeEntity>()
                                 .AndWhere(p => p.UserNo == inputInfo.UserNo).Single();

                    if (employeeInfo.IsNotNull())
                    {
                        var result = tran.CommandSql<int>().BySql(DataCommandManager.GetSQLCommand("SettlementEmployeeSalary", "Employee"),
                             new
                             {
                                 PayMethodID = inputInfo.PayMethodID,
                                 PayMethodName = inputInfo.PayMethodName,
                                 MyYear = inputInfo.MyYear,
                                 MyMonth = inputInfo.MyMonth,
                                 IsLeave = employeeInfo.ZTBS == true ? 0 : 1,
                                 Question = !inputInfo.Question,
                                 Rmark = inputInfo.Rmark,
                                 BegTime = inputInfo.BegTime,
                                 EndTime = inputInfo.EndTime,
                                 CheckProjectNo = inputInfo.CheckProjectNo.IsNotNullOrEmpty() ? inputInfo.CheckProjectNo : null,
                                 FNO = GetNumber().ToString(),
                                 UserNo = inputInfo.UserNo,
                                 Name = inputInfo.Name,
                                 PYM = inputInfo.PYM,
                                 Sex = inputInfo.Sex.Trim() == "1" ? "男" : (inputInfo.Sex.Trim() == "2" ? "女" : "未知"),
                                 Code = inputInfo.Code,
                                 BankNo = inputInfo.BankNo,
                                 BankName = inputInfo.BankName,
                                 DepNo = loginUser.OrganizationCode,
                                 WorkTypeID = inputInfo.WorkTypeID,
                                 WorkTypeName = inputInfo.WorkTypeName,
                                 WorkDay = inputInfo.WorkDay,
                                 NoWorkDay = inputInfo.NoWorkDay,
                                 DayMoney = employeeInfo.DayMoney,
                                 OverTime = inputInfo.OverTime,
                                 OverMoney = employeeInfo.OverMoney,
                                 TOverMoney = inputInfo.TOverMoney,
                                 LoanMoney = inputInfo.LoanMoney,
                                 LoanBackMoney = inputInfo.LoanBackMoney,
                                 AmountMoney = inputInfo.AmountMoney,
                                 TWorkMoney = inputInfo.TWorkMoney,
                                 RealMoney = inputInfo.RealMoney,
                                 CreateUserNo = loginUser.Code,
                                 CreateUserName = loginUser.LoginName,
                             }).ExecuteNoQuery();

                        if (result > 0)
                        {
                            resultInfo.Code = ActionCodes.Success;
                            resultInfo.Data = result;
                            resultInfo.Msg = "操作成功！";
                        }
                        else
                        {
                            resultInfo.Msg = "操作失败！";
                        }
                    }
                    else
                    {
                        resultInfo.Msg = "操作失败,无对应员工信息！";
                    }
                });
            }
            return resultInfo;

        }

        /// <summary>
        /// 获取员工工资单信息【已结工资】
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<T_SalaryCountEntity>> LoadEmployeeSalaryRecordList(ParametersInfo<SalaryRecordQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<T_SalaryCountEntity>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                var result = con.QuerySet<T_SalaryCountEntity>().WithNoLock();

                #region 条件

                if (inputInfo.parameters.IsNotNull())
                {
                    if (inputInfo.parameters.Name.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.Name.Contains(inputInfo.parameters.Name));
                    }
                    if (inputInfo.parameters.PYM.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.PYM.Contains(inputInfo.parameters.PYM));
                    }
                    if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.ProjectNo.Contains(inputInfo.parameters.ProjectNo));
                    }
                    if (inputInfo.parameters.YearMonth.IsNotNullOrEmpty())
                    {
                        var yearAndMonth = inputInfo.parameters.YearMonth.Split('-');
                        string yearInfo = yearAndMonth[0];
                        string monthInfo = int.Parse(yearAndMonth[1]).ToString();
                        result = result.AndWhere(p => p.MyYear == yearInfo && p.MyMonth == monthInfo);
                    }
                }
                #endregion

                result.AndWhere(p => p.DepNo == loginUser.OrganizationCode);

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field)
                  .PageList(inputInfo.page, inputInfo.limit);

                if (listInfo.Data.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.Data.ToList();
                    resultInfo.Count = listInfo.Total;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取所有员工工资单信息
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<T_SalaryCountEntity>> LoadSalaryRecordAllList(ParametersInfo<SalaryRecordQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<T_SalaryCountEntity>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                var result = con.QuerySet<T_SalaryCountEntity>().WithNoLock();

                #region 条件

                if (inputInfo.parameters.IsNotNull())
                {
                    if (inputInfo.parameters.Name.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.Name.Contains(inputInfo.parameters.Name));
                    }
                    if (inputInfo.parameters.PYM.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.PYM.Contains(inputInfo.parameters.PYM));
                    }
                    if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.ProjectNo.Contains(inputInfo.parameters.ProjectNo));
                    }
                    if (inputInfo.parameters.YearMonth.IsNotNullOrEmpty())
                    {
                        var yearAndMonth = inputInfo.parameters.YearMonth.Split('-');
                        string yearInfo = yearAndMonth[0];
                        string monthInfo = int.Parse(yearAndMonth[1]).ToString();
                        result = result.AndWhere(p => p.MyYear == yearInfo && p.MyMonth == monthInfo);
                    }
                }
                #endregion

                result.AndWhere(p => p.DepNo == loginUser.OrganizationCode);

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field)
                  .Multiple();

                if (listInfo.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.ToList();
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 撤销工资单
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> CancelSalary(SalaryCancelRequest inputInfo)
        {

            var resultInfo = new ResultJsonInfo<int>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                con.Transaction(tran =>
                {
                    var employeeInfo = tran.QuerySet<T_EmployeeEntity>()
                                 .AndWhere(p => p.UserNo == inputInfo.UserNo).Single();

                    if (employeeInfo.IsNotNull())
                    {
                        var result = tran.CommandSql<int>().BySql(DataCommandManager.GetSQLCommand("CanceEmployeeSalary", "Employee"),
                             new
                             {
                                 UserNo = inputInfo.UserNo,
                                 FNO = inputInfo.FNO,
                                 DepNo = loginUser.OrganizationCode,
                                 DataId = 0,
                                 FlowTypeID = 0
                             }).ExecuteNoQuery();

                        if (result > 0)
                        {
                            resultInfo.Code = ActionCodes.Success;
                            resultInfo.Data = result;
                            resultInfo.Msg = "操作成功！";
                        }
                        else
                        {
                            resultInfo.Msg = "操作失败！";
                        }
                    }
                    else
                    {
                        resultInfo.Msg = "操作失败,无对应员工信息！";
                    }
                });
            }
            return resultInfo;

        }

        /// <summary>
        /// 获取员工工资发放信息【工资发放】
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<T_SalaryCountEntity>> LoadEmployeeSalaryPaymentList(ParametersInfo<SalaryPaymentQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<T_SalaryCountEntity>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                var result = con.QuerySet<T_SalaryCountEntity>().WithNoLock();

                #region 条件

                if (inputInfo.parameters.IsNotNull())
                {
                    if (inputInfo.parameters.KeyWord.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.Name.Contains(inputInfo.parameters.KeyWord)
                                            || p.PYM.Contains(inputInfo.parameters.KeyWord)
                                            || p.FNO.Contains(inputInfo.parameters.KeyWord));
                    }
                    if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.ProjectNo.Contains(inputInfo.parameters.ProjectNo));
                    }
                    if (inputInfo.parameters.YearMonth.IsNotNullOrEmpty())
                    {
                        var yearAndMonth = inputInfo.parameters.YearMonth.Split('-');
                        string yearInfo = yearAndMonth[0];
                        string monthInfo = int.Parse(yearAndMonth[1]).ToString();
                        result = result.AndWhere(p => p.MyYear == yearInfo && p.MyMonth == monthInfo);
                    }
                    switch (inputInfo.parameters.iType)
                    {
                        case 11:
                            result = result.AndWhere(p => p.FState == 0 && p.Question == 0);
                            break;
                        case 22:
                            result = result.AndWhere(p => p.FState == 1 && p.Question == 0);
                            break;
                        case 33:
                            result = result.AndWhere(p => p.Question == 1);
                            break;
                    }
                }
                #endregion

                result.AndWhere(p => p.DepNo == loginUser.OrganizationCode);

                var listInfo = result.OrderBy(inputInfo.order == "asc" ? OrderByType.Asc : OrderByType.Desc, inputInfo.field)
                  .PageList(inputInfo.page, inputInfo.limit);

                if (listInfo.Data.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo.Data.ToList();
                    resultInfo.Count = listInfo.Total;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 员工工资发放所有信息列表
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<List<T_SalaryCountEntity>> LoadEmployeeSalaryPaymentAllList(ParametersInfo<SalaryPaymentQueryRequest> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<List<T_SalaryCountEntity>>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                StringBuilder whereStr = new StringBuilder();

                var result = con.QuerySet<T_SalaryCountEntity>().WithNoLock();

                #region 条件

                if (inputInfo.parameters.IsNotNull())
                {
                    if (inputInfo.parameters.KeyWord.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.Name.Contains(inputInfo.parameters.KeyWord)
                                            || p.PYM.Contains(inputInfo.parameters.KeyWord)
                                            || p.FNO.Contains(inputInfo.parameters.KeyWord));
                    }
                    if (inputInfo.parameters.ProjectNo.IsNotNullOrEmpty())
                    {
                        result = result.AndWhere(p => p.ProjectNo.Contains(inputInfo.parameters.ProjectNo));
                    }
                    if (inputInfo.parameters.YearMonth.IsNotNullOrEmpty())
                    {
                        var yearAndMonth = inputInfo.parameters.YearMonth.Split('-');
                        string yearInfo = yearAndMonth[0];
                        string monthInfo = int.Parse(yearAndMonth[1]).ToString();
                        result = result.AndWhere(p => p.MyYear == yearInfo && p.MyMonth == monthInfo);
                    }
                    switch (inputInfo.parameters.iType)
                    {
                        case 11:
                            result = result.AndWhere(p => p.FState == 0 && p.Question == 0);
                            break;
                        case 22:
                            result = result.AndWhere(p => p.FState == 1 && p.Question == 0);
                            break;
                        case 33:
                            result = result.AndWhere(p => p.Question == 1);
                            break;
                    }
                }
                #endregion

                var listInfo = result.AndWhere(p => p.DepNo == loginUser.OrganizationCode).Multiple();

                if (listInfo.IsNotNull())
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = listInfo;
                }
                else
                {
                    resultInfo.Msg = "无对应信息！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 修改补助
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> SaveSubsidy(SalarySubsidyRequest inputInfo)
        {

            var resultInfo = new ResultJsonInfo<int>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = con.CommandSet<T_SalaryCountEntity>().AndWhere(a => a.ID == inputInfo.ID)
                          .UpdateByExp(p => new T_SalaryCountEntity
                          {
                              CarBT = inputInfo.CarBT,
                              PhoneBT = inputInfo.PhoneBT,
                              BonusBT = inputInfo.BonusBT,
                              OtherBT1 = inputInfo.OtherBT1,
                              OtherBT2 = inputInfo.OtherBT2
                          });

                if (result > 0)
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = result;
                    resultInfo.Msg = "操作成功！";
                }
                else
                {
                    resultInfo.Msg = "操作失败！";
                }
            }
            return resultInfo;

        }

        /// <summary>
        /// 工资发放
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> PaymentSalary(List<int> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<int>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = con.CommandSet<T_SalaryCountEntity>().AndWhere(a => inputInfo.Contains(a.ID))
                          .UpdateByExp(p => new T_SalaryCountEntity
                          {
                              FState = 1
                          });

                if (result > 0)
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = result;
                    resultInfo.Msg = "操作成功！";
                }
                else
                {
                    resultInfo.Msg = "操作失败！";
                }
            }
            return resultInfo;

        }

        /// <summary>
        /// 撤销工资发放
        /// </summary>
        /// <param name="inputInfo"></param>
        /// <returns></returns>
        public ResultJsonInfo<int> RevokeSalary(List<int> inputInfo)
        {

            var resultInfo = new ResultJsonInfo<int>();
            var loginUser = UserSession.GetLoginUser<SysUserInfo>();

            using (var con = MsSqlDataBase.GetConnection(DatabaseName.ErpDB_MsSql))
            {
                var result = con.CommandSet<T_SalaryCountEntity>().AndWhere(a => inputInfo.Contains(a.ID))
                          .UpdateByExp(p => new T_SalaryCountEntity
                          {
                              FState = 0
                          });

                if (result > 0)
                {
                    resultInfo.Code = ActionCodes.Success;
                    resultInfo.Data = result;
                    resultInfo.Msg = "操作成功！";
                }
                else
                {
                    resultInfo.Msg = "操作失败！";
                }
            }
            return resultInfo;
        }

        #endregion

        #region 获取员工编码或工资单编码

        //共享锁
        private static object NumberLock = new object();

        /// <summary>
        /// 获取员工编码或工资单编码
        /// </summary>
        /// <returns></returns>
        private static long GetNumber()
        {
            StringBuilder sb = new StringBuilder();
            lock (NumberLock)
            {
                string newBase = DateTime.Now.ToString("yyMMddHHmmssfff");

                string noIndex = RedisClient.GetRedisDb(ConstRedisKey.RedisDbWeb)
                    .StringGet(ConstRedisKey.RedisEmployeeCode);

                if (noIndex.IsNullOrEmpty() || noIndex == "null")
                {
                    int index = NumeralHelper.Next(90) + 10;
                    RedisClient.GetRedisDb(ConstRedisKey.RedisDbWeb)
                        .StringSet(ConstRedisKey.RedisEmployeeCode, index.ToString());
                    sb.Append(newBase + index);
                }
                else
                {
                    if (int.Parse(noIndex) < 99)
                    {
                        var index = RedisClient.GetRedisDb(ConstRedisKey.RedisDbWeb).SetStringIncrement(ConstRedisKey.RedisEmployeeCode);
                        sb.Append(newBase + index);
                    }
                    else
                    {
                        var index = RedisClient.GetRedisDb(ConstRedisKey.RedisDbWeb).StringSet(ConstRedisKey.RedisEmployeeCode, "10");
                        sb.Append(newBase + 10);
                    }
                }
            }
            return long.Parse(sb.ToString());
        }
        #endregion
    }
}
